define(['jquery','template','module/rightClickControl'], function($,template,rightClickControl) {
	var tabNav= oUl = $("#min_title_list");
	var hide_nav=$("#portal-tabNavbar");
	var portal_pages=$("#portal-pages");
	var topWindow=$(window.parent.document);
	var num=0;
	var panel = $("#portal-panel");
	/*暴露给外部的方法*/
	var pageControl = {
		init:function(){

			//右键关闭
			initRightClick();

			//添加自定义跳转到页面的形式
			$(document).on("click",".iframe-href",function(){
				pageControl.openPage(this);
			});
			//切换页面
			$(document).on("click","#min_title_list li",function(){
				var bStopIndex=$(this).index();
				
				$("#min_title_list li").removeClass("active").eq(bStopIndex).addClass("active");
				portal_pages.find(".portal-page").hide().eq(bStopIndex).show().addClass("active");
			});
			//删除页面
			$(document).on("click","#min_title_list li i",function(){
				var aCloseIndex=$(this).parents("li").index();
				removePage(aCloseIndex);
			});
			//删除页面
			$(document).on("dblclick","#min_title_list li",function(){
				
				var aCloseIndex=$(this).index();
				removePage(aCloseIndex);
			});
			//页面大小切换
			$(document).on("click","#portal-toggle",function(){
				 
				var flag = panel.hasClass('portal-panel-miniSize');
				if(flag){
					openPanel();		
				}else{
					closePanel();
				}

			});

			//全屏切换
			$(document).on("click","#portal_fullToggle",function(){
				//var _left = $("#portal-panel").hasClass('portal-panel-miniSize')?50:220;
				var target = $("#portal-content");
				var t = $(this);
				if(target.hasClass('portal-full')){
					t.find('.iconfont').addClass('icon-fullpage').removeClass('icon-minipage');
					target.stop().animate({
						'top': '50px',
						
						'z-index': 2},
						'fast', function() {
						// target.removeAttr('style');

						target.removeClass('portal-full');
					});
				}else{
					closePanel();
					t.find('.iconfont').removeClass('icon-fullpage').addClass('icon-minipage');
					target.css('z-index',6).stop().animate({
						'top': '0',
						'z-index': 6},
						'fast', function() {
						target.removeAttr('style');	
						target.addClass('portal-full');
					});
				}
			});
			/*//关闭全部页面
			$(document).on("click","#portal_closeAll",function(){
				removeAllIframe();
			});
			//关闭右侧页面
			$(document).on("click","#portal_closeRight",function(){
				var li = $("#min_title_list");
				
				var idx = li.find(".active").index();
				
				removeRightPage(idx);
			});*/

			$("#portal_close_btns").on("click","a",function(){
				var id = $(this).attr("id");
				var li = $("#min_title_list");
				var idx = li.find(".active").index();
				
				switch(id){
					case 'portal_closeCur' : removePage(idx); break;
					case  'portal_closeLeft' :removeLeftPage(idx); break;
					case  'portal_closeRight' :removeRightPage(idx); break;
					case  'portal_closeAll' :removeAllIframe(); break;
					case  'portal_closeOther' :
							removeOtherPage(idx);

						break;
					default: return false;
				}

				return false;
			});

			$('#portal-tabNav-next').click(function(){
				
				num==oUl.find('li').length-1?num=oUl.find('li').length-1:num++;
				toNavPos();
			});
			$('#portal-tabNav-prev').click(function(){
				num==0?num=0:num--;
				toNavPos();
			});

			tabNavallwidth();

		},
		createPage:function(href,titleName){
			creatIframe(href,titleName);
		},
		removePage:function(pagename,callback,params){
			removeIframe(pagename,callback,params);
		},
		openPage:function(obj){
			if($(obj).attr('_href')){
			var bStop=false;
			var bStopIndex=0;
			var _href=$(obj).attr('_href');
			var _titleName=$(obj).attr("data-title");
			
			var show_navLi=topWindow.find("#min_title_list li");
			show_navLi.each(function() {
				if($(this).find('span').attr("data-href")==_href){
					bStop=true;
					bStopIndex=show_navLi.index($(this));
					return false;
				}
			});
			if(!bStop){
				creatIframe(_href,_titleName);
				min_titleList();
			}
			else{
				show_navLi.removeClass("active").eq(bStopIndex).addClass("active");
				portal_pages.find(".portal-page").hide().eq(bStopIndex).addClass("active").show().find("iframe").attr("src",_href);
			}
		}
		}
	};

	/*页面相关*/
	function openPanel(){
		$("#portal-detail").stop().animate({
			'left': '220px',
			
		},'fast');
		$("#portal-side").stop().animate({
			'width': '220px',
			
		},'fast', function() {
			panel.removeClass('portal-panel-miniSize');
			$("#portal-side .sidebar.open .sidebar-children").show();
		});
	}

	function closePanel() {
		$("#portal-side .sidebar.open .sidebar-children").hide();

		$("#portal-side").addClass('hideObj').stop().animate({
			'width': '50px',
			
		},'fast', function() {
			panel.addClass('portal-panel-miniSize');
			$("#portal-side").removeClass('hideObj')
			
		});
		$("#portal-detail").stop().animate({
			'left': '50px',
			
		},'fast');
	}

	function min_titleList(){
		
		var show_nav=topWindow.find("#min_title_list");
		var aLi=show_nav.find("li");
	};
	function creatIframe(href,titleName){
		
		var show_nav=topWindow.find('#min_title_list');
		show_nav.find('li').removeClass("active");
		
		show_nav.append('<li class="active"><span data-href="'+href+'">'+titleName+'</span><i class="icon iconfont icon-close"></i></li>');
		
		tabNavallwidth();

		var iframeBox= portal_pages.find('.portal-page');
		iframeBox.hide();
		 portal_pages.append('<div class="portal-page active"><div class="loading"></div><iframe frameborder="0" src='+href+'></iframe></div>');
		var showBox= portal_pages.find('.portal-page:visible');
		showBox.find('iframe').load(function(){
			showBox.find('.loading').hide();
		});

		focusTab();
	}
	/*function removeIframe(){
		var topWindow = $(window.parent.document);
		
		var showTab = topWindow.find(".acrossTab li.active");
		var showBox=topWindow.find('.portal-page:visible');
		var i = showTab.index();
		tab.eq(i-1).addClass("active");
		iframe.eq(i-1).show();
		tab.eq(i).remove();
		iframe.eq(i).remove();

		tabNavallwidth();
	}*/

	/*
		
		传入参数:
			pagename:关闭页面后,跳转到的tab的标题
			callback:跳转到后,需要执行的回调函数 *非必要
			params:回调函数的传参 *非必要 暂时只支持传一个参数,多参数有bug,待解决
	*/
	function removeIframe(pagename,callback,params){
		var topWindow = $(window.parent.document);
		var iframe = topWindow.find('#portal-pages .portal-page');
		var tab = topWindow.find(".acrossTab li");
		var showTab = topWindow.find(".acrossTab li.active");
		var showBox=topWindow.find('.portal-page:visible');
		var i = showTab.index();

		if(pagename){
			var tabIndex = -1;
			tab.each(function(index, el) {
				var t = $(this);
				var span= t.find('span');
				if(span.text()==pagename){
					tabIndex = index;
				}
			});
			if(tabIndex>= 0){
				tab.eq(tabIndex).addClass("active");
				iframe.eq(tabIndex).show();
				if(callback){
					var ifr = document.getElementsByTagName('iframe')[tabIndex];
					var win = ifr.window || ifr.contentWindow;

					win.pageCallback(callback,params);
				}

			}else{
				tab.eq(i-1).addClass("active");
				iframe.eq(i-1).show();
			}
			
		}else{
			tab.eq(i-1).addClass("active");
			iframe.eq(i-1).show();
		}
		tab.eq(i).remove();
		iframe.eq(i).remove();	
		
	}

	/*关闭所有iframe*/
	function removeAllIframe(){
		var flag = confirm("是否关闭所有窗口");
		if(flag){
				var topWindow = $(window.parent.document);
				var iframe = topWindow.find('#portal-pages .portal-page');
				var tab = topWindow.find(".acrossTab li");

				var otherIframe = topWindow.find('#portal-pages .portal-page:gt(0)');
				var othertab = topWindow.find('.acrossTab li:gt(0)');
				tab.eq(0).addClass("active");
				iframe.eq(0).show();
				otherIframe.remove();
				othertab.remove();

				var box = $("#min_title_list");
				box.stop().animate({'left':0},100);
		}
		
	}

	function removeRightPage(index){
		var flag = confirm("是否关闭当前页面右侧页面?");
		if(flag){
				var topWindow = $(window.parent.document);
				var iframe = topWindow.find('#portal-pages .portal-page');
				var tab = topWindow.find(".acrossTab li");

				var otherIframe = topWindow.find('#portal-pages .portal-page:gt('+index+')');
				var othertab = topWindow.find('.acrossTab li:gt('+index+')');
				
				otherIframe.remove();
				othertab.remove();

				tabNavallwidth();
		}
	}

	function removeLeftPage(index){
		if(index == 0) return false;

		var flag = confirm("是否关闭当前页面左侧页面?");
		if(flag){
				var topWindow = $(window.parent.document);
				var iframe = topWindow.find('#portal-pages .portal-page');
				var tab = topWindow.find(".acrossTab li");

				var otherIframe = topWindow.find('#portal-pages .portal-page:lt('+index+'):gt(0)');
				var othertab = topWindow.find('.acrossTab li:lt('+index+'):gt(0)');
				
				otherIframe.remove();
				othertab.remove();

				tabNavallwidth();
		}
	}
	function removeOtherPage(index){
		var flag = confirm("是否关闭当前页面外的其他页面?");
		if(flag){
				var topWindow = $(window.parent.document);
				var iframe = topWindow.find('#portal-pages .portal-page');
				var tab = topWindow.find(".acrossTab li");

				var otherIframe = topWindow.find('#portal-pages .portal-page:lt('+index+'):gt(0)');
				var othertab = topWindow.find('.acrossTab li:lt('+index+'):gt(0)');
				
				var otherIframe2 = topWindow.find('#portal-pages .portal-page:gt('+index+')');
				var othertab2 = topWindow.find('.acrossTab li:gt('+index+')');
				
				otherIframe2.remove();
				othertab2.remove();


				otherIframe.remove();
				othertab.remove();

				tabNavallwidth();
		}
	}

	function removePage(index){
		if(index == 0) return false;
		var iframe = portal_pages.find('.portal-page');
		var tab = tabNav.find("li");
		tab.eq(index-1).addClass("active");
		iframe.eq(index-1).show().addClass("active");
		tab.eq(index).remove();
		iframe.eq(index).remove();

		tabNavallwidth();
	}
	/*获取顶部选项卡总长度*/
	function tabNavallwidth(){
		var taballwidth=0,
			$tabNav = hide_nav.find(".acrossTab"),
			$tabNavWp = hide_nav.find(".portal-tabNav-wp"),
			$tabNavitem = hide_nav.find(".acrossTab li"),
			$tabNavmore =hide_nav.find("#portal-tabNav-prev,#portal-tabNav-next");
		if (!$tabNav[0]){return}
		$tabNavitem.each(function(index, element) {
	        taballwidth+=Number(parseFloat($(this).outerWidth()+3))
	    });
		$tabNav.width(taballwidth+25);
		var w = $tabNavWp.width();
		if(taballwidth+25>w){
			$tabNavmore.show()}
		else{
			$tabNavmore.hide();
			$tabNav.css({left:0})
		}
	}

	function focusTab(){
		var left =  $("#min_title_list li.active").position().left;
		var w = $(".portal-tabNav-wp").width();
		var w2 = $("#min_title_list li.active").outerWidth()
		var offset = w-left;
		//console.log(offset);
		if(offset>=0){
			return;
		}else{
			$("#min_title_list").stop().animate({'left':offset-w2},100);
		}
	}

	function rightMenuInit(){
		
	}
	
	/*选项卡移动*/
	function toNavPos(){
		var left = $("#min_title_list").css('left').replace('px','');
		var _num = Math.ceil(Math.abs(left)/100);

		var __num = __num>0?_num:num;
		console.log(__num);
		var box = $("#min_title_list");
		var box2 = $("#portal-tabNavbar");
		var maxMove = box2.width()/box.width();
		box.stop().animate({'left':-__num*100},100);

		// var w = $(".portal-tabNav-wp")
	}


	/*右键事件*/
	function initRightClick(){
		var _lastRightClick = -1;

		$(document).on("mousedown","#min_title_list>li",function(e){
			if(3 == e.which){
				_lastRightClick = $(this).index();
			}
		});

		// 右键事件
		/*case '' : removePage(idx); break;
		case  'portal_closeLeft' :removeLeftPage(idx); break;
		case  'portal_closeRight' :removeRightPage(idx); break;
		case  'portal_closeAll' :removeAllIframe(); break;
		case  'portal_closeOther' :*/

		var portal_closeCur = {text: '关闭当前页面', action: function(e){
				e.preventDefault();
				removePage(_lastRightClick);
			}};
		var portal_closeAll = {text: '关闭全部页面', action: function(e){
				e.preventDefault();
				removeAllIframe();
			}};
		var portal_closeOther = {text: '关闭其他页面', action: function(e){
				e.preventDefault();
				removeOtherPage(_lastRightClick);
			}};
		var portal_closeLeft = {text: '关闭左侧页面', action: function(e){
				e.preventDefault();
				removeLeftPage(_lastRightClick);
			}};
		var portal_closeRight = {text: '关闭右侧页面', action: function(e){
				e.preventDefault();
				removeRightPage(_lastRightClick);
				
			}};
		rightClickControl.context.init({preventDoubleContext: false});

		rightClickControl.context.attach('#min_title_list>li:gt(0)', [
			portal_closeCur,portal_closeAll,portal_closeOther,portal_closeLeft,portal_closeRight			
		]);

		rightClickControl.context.attach('#min_title_list>li:eq(0)', [
			portal_closeOther		
		]);
		
		rightClickControl.context.settings({compress: true});
	}

	return {
		pageControl : pageControl
	}
});